package controllers;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Profil;
import system.Controller;
import system.Helper;


/**
 *
 * @author Rezan Achmad
 */
@WebServlet(name = "ProfilController", urlPatterns = {"/profil", "/profil/", "/profil/view", "/profil/admin", "/profil/index_uploadfoto"})
public class ProfilController extends Controller {
    
    @Override
    protected void initRules() {
        rules.put("/profil/admin", identity.isAdmin());
        rules.put("/profil/index_uploadfoto", identity.isAdmin());
    }
    
//    Boolean isThere = false;
//    private Profil myprofil = new Profil();

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userPath = request.getServletPath();

        if (userPath.equals("/profil") || userPath.equals("/profil/")) {
            // TODO : to index page
            userPath = "/profil/view";
            setView(userPath, request, response);
        } else if (userPath.equals("/profil/view")) {
            // TODO : to registrasi page
            actionView(request, response);

        }  else if (userPath.equals("/profil/admin") || userPath.equals("/profil/index_uploadfoto")) {
            // TODO : to registrasi page
            actionAdmin(request, response);
        }
    }

    protected void actionView(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String userPath = request.getServletPath();
        request.setAttribute("profil", Profil.getCurrentProfil());
        
        setView(userPath, request, response);
    }

    protected void actionAdmin(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //fill in the blank space first
        String userPath = request.getServletPath();
        Profil myprofil = Profil.getCurrentProfil();
        request.setAttribute("profil", myprofil);

        if (isGet) {
            // TODO get

        } else if (isPost) {
            // TODO post
            if (userPath.equals("/profil/admin")) {
                
                String nama = request.getParameter("nama_toko");
                String deskripsi = request.getParameter("deskripsi");
                String welcome_msg = request.getParameter("welcome_msg");
                String alamat = request.getParameter("alamat");
                String telepon = request.getParameter("telepon");
                String email = request.getParameter("email");
                Integer diskon = Integer.parseInt(request.getParameter("diskon"));

                myprofil.nama_toko = (nama != null) ? nama : "";
                myprofil.deskripsi = (deskripsi != null) ? deskripsi : "";
                myprofil.welcome_msg = (welcome_msg != null) ? welcome_msg : "";
                myprofil.alamat = (alamat != null) ? alamat : "";
                myprofil.telepon = (telepon != null) ? telepon : "";
                myprofil.email = (email != null) ? email : "";
                myprofil.diskon = (diskon != null) ? diskon : 0;

//                if (((myprofil.deskripsi).equals("")) || ((myprofil.nama_toko).equals(""))) {
//                    setView("/profil/admin", request, response);
//                    return;
//                }
                
            } else if (userPath.equals("/profil/index_uploadfoto")) {
                myprofil.path_logo = Helper.uploadFile(request, "path_logo");   
            }

            myprofil.update();
            userPath = "/profil/view";
        }

        setView(userPath, request, response);
    }
}
